//
//  HYInfoCommentCell.m
//  HaiYue
//
//  Created by skyline on 2018/2/6.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYInfoCommentCell.h"
#import "HYNetizenView.h"
NSString *const HYInfoCommentCellCai = @"HYInfoCommentCellCai";
NSString *const HYInfoCommentCellZan = @"HYInfoCommentCellZan";
NSString *const HYInfoCommentCellReply = @"HYInfoCommentCellReply";
NSString *const HYInfoCommentCellCallBack = @"HYInfoCommentCellCallBack";

@interface HYInfoCommentCell()
@property (nonatomic, strong) HYNetizenView *netizenView;
@property (nonatomic, strong) HYLabel *commentLBL;
@property (nonatomic, strong) HYReplyBtn *zanBtn;
@property (nonatomic, strong) HYReplyBtn *caiBtn;
@property (nonatomic, strong) HYReplyBtn *huifuBtn;
@property (nonatomic, strong) HYButton *showAllBtn;
@property (nonatomic, strong) HYView *line;

@property (nonatomic, assign) CGFloat tempH;
@end
@implementation HYInfoCommentCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.style = HYInfoCommentCellStyleDefault;
        [CCNotice addObs:self sel:@selector(actionForCallback:) name:HYInfoCommentCellCallBack obj:self];
    }
    return self;
}

- (HYView *)line
{
    if (!_line)
    {
        _line = [[HYView alloc] initWithColor:CELL_LINE_COLOR];
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.commentModel)
                make.left.equalTo(self);
            else
                make.left.equalTo(self).with.offset(HYNetizenHeight + GAP * 2);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _line;
}


- (HYNetizenView *)netizenView
{
    if (!_netizenView)
    {
        _netizenView  = [[HYNetizenView alloc] init];
        [self addSubview:_netizenView];
        [_netizenView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(GAP);
            if (self.commentModel)
                make.left.equalTo(self);
            else
                make.left.equalTo(self).with.offset(GAP);
            make.right.equalTo(self);
            make.height.mas_equalTo(HYNetizenHeight);
        }];
    }
    return _netizenView;
}

- (HYLabel *)commentLBL
{
    if (!_commentLBL)
    {
        _commentLBL = [[HYLabel alloc] initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:DefaultFont weight:UIFontWeightLight]];
        _commentLBL.fixedWidth = SCREEN_WIDTH - HYNetizenHeight - GAP * 3.0;
        [self addSubview:_commentLBL];
        [_commentLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.netizenView.mas_bottom).with.offset(GAP);
            make.left.equalTo(self).with.offset(HYNetizenHeight + GAP * 2);
        }];
    }
    return _commentLBL;
}

- (HYReplyBtn *)zanBtn
{
    if (!_zanBtn)
    {
        _zanBtn = [[HYReplyBtn alloc] init];
        _zanBtn.imageName = @"zan";
        _zanBtn.selectImageName = @"zan_selected";
        _zanBtn.title = @"0";
        [_zanBtn addTarget:self action:@selector(actionForDianzan) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_zanBtn];
        [_zanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.caiBtn.mas_left).with.offset(-GAP);
            make.top.equalTo(self.commentLBL.mas_bottom);
        }];
    }
    return _zanBtn;
}

- (HYReplyBtn *)caiBtn
{
    if (!_caiBtn)
    {
        _caiBtn = [[HYReplyBtn alloc] init];
        _caiBtn.imageName = @"cai";
        _caiBtn.selectImageName = @"cai_selected";
        _caiBtn.title = @"0";
        [_caiBtn addTarget:self action:@selector(actionForCai) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_caiBtn];
        [_caiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.commentModel && self.style == HYInfoCommentCellStyleDefault)
                make.right.equalTo(self.huifuBtn.mas_left).with.offset(-GAP);
            else
                make.right.equalTo(self).with.offset(-GAP);
            make.top.equalTo(self.commentLBL.mas_bottom);
        }];
    }
    return _caiBtn;
}

- (HYReplyBtn *)huifuBtn
{
    if (!_huifuBtn)
    {
        _huifuBtn = [[HYReplyBtn alloc] init];
        _huifuBtn.imageName = @"reply";
        _huifuBtn.title = @"0";
        [_huifuBtn addTarget:self action:@selector(actionForHuifu) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_huifuBtn];
        [_huifuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-GAP);
            make.top.equalTo(self.commentLBL.mas_bottom);
        }];
    }
    return _huifuBtn;
}
- (HYButton *)showAllBtn
{
    if (!_showAllBtn)
    {
        _showAllBtn = [[HYButton alloc] init];
        _showAllBtn.font = [UIFont systemFontOfSize:LittleFont weight:UIFontWeightLight];
        [_showAllBtn addTarget:self action:@selector(actionForHuifu) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_showAllBtn];
        [_showAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-GAP);
            make.bottom.equalTo(self).with.offset(-GAP);
        }];
    }
    return _showAllBtn;
}

- (void)setCommentModel:(HYCommentModel *)commentModel
{
    if (_commentModel != commentModel)
    {
        _commentModel = commentModel;
        self.netizenView.commentModel = self.commentModel;
        self.commentLBL.text = self.commentModel.content;
        self.zanBtn.title = self.commentModel.likecount;
        self.caiBtn.title = self.commentModel.unlikecount;
        if (self.style == HYInfoCommentCellStyleDefault)
            self.huifuBtn.title = self.commentModel.replycount;
        self.zanBtn.selected = [self.commentModel.mystate hasPrefix:@"1"]?YES:NO;
        self.caiBtn.selected = [self.commentModel.mystate hasPrefix:@"-1"]?YES:NO;
        self.line.hidden = NO;
        [self setupSubReply];
        [self updateBounds];
        [self setupShowAllBtn];
    }
}

- (void)setReplyModel:(HYReplyModel *)replyModel
{
    if (_replyModel != replyModel)
    {
        _replyModel = replyModel;
        self.netizenView.replyModel = _replyModel;
        self.commentLBL.text = _replyModel.content;
        self.zanBtn.title = _replyModel.likecount;
        self.caiBtn.title = _replyModel.unlikecount;
        self.zanBtn.selected = [_replyModel.mystate hasPrefix:@"1"]?YES:NO;
        self.caiBtn.selected = [_replyModel.mystate hasPrefix:@"-1"]?YES:NO;
        self.line.hidden = NO;
        [self updateBounds];
    }
}
- (void)setupShowAllBtn
{
    NSString * text = [NSString stringWithFormat:@"全部%d条回复>>",[self.commentModel.replycount intValue]];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text];
    [attr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(0, text.length)];
    [attr addAttribute:NSFontAttributeName value:self.showAllBtn.font range:NSMakeRange(0, text.length)];
    self.showAllBtn.attr = attr;
    self.showAllBtn.hidden = ([self.commentModel.replycount intValue] > 3 && self.style == HYInfoCommentCellStyleDefault)?NO:YES;
    self.bounds = self.showAllBtn.hidden?self.bounds:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height + self.showAllBtn.bounds.size.height - GAP / 2);
}
- (void)setupSubReply
{
    if (self.style == HYInfoCommentCellStyleSimple)
        return;
    for (UIView *view in [self subviews])
    {
        self.tempH = 0;
        if (view.tag >= 300)
        {
            [view removeFromSuperview];
        }
    }
    int count = [self.commentModel.replycount intValue]>3?3:(int)self.commentModel.replydatas.count;
    for (int i = 0; i < count; i++)
    {
        HYReplyModel *model = self.commentModel.replydatas[i];
        
        HYLabel *label = [[HYLabel alloc] initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:LittleFont weight:UIFontWeightLight]];
        label.fixedWidth = SCREEN_WIDTH - HYNetizenHeight - GAP * 3.0;
        
        NSString  *string = [NSString stringWithFormat:@"%@:  %@", model.publisher, model.content];
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
        [attr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(0, model.publisher.length)];
        [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:LittleFont weight:UIFontWeightLight] range:NSMakeRange(0, model.publisher.length)];
        [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:LittleFont weight:UIFontWeightLight] range:NSMakeRange(model.publisher.length + 2, model.content.length)];
        [attr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, model.publisher.length)];
        [attr addAttribute:NSUnderlineColorAttributeName value:THEME_COLOR range:NSMakeRange(0, model.publisher.length)];
        
        label.attributedText =  attr;
        label.tag = 300 + i;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.zanBtn.mas_bottom).with.offset(GAP + self.tempH);
            make.left.equalTo(self.commentLBL);
        }];
        self.tempH = CGRectGetHeight(label.bounds) + GAP + self.tempH;
    }
}
- (void)updateBounds
{
    self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, HYNetizenHeight + self.commentLBL.bounds.size.height + self.zanBtn.bounds.size.height + GAP * 4 + self.tempH);
}

//点赞 1点赞 0 取消 -1踩
- (void)actionForDianzan
{
    if ([[HYUserManager manager].user.account length] <= 1)
        return [[[self superViewController] navigationController] pushViewController:[NSClassFromString(@"HYLoginViewController") new] animated:YES];
    if (self.commentModel)
        [CCNotice post:HYInfoCommentCellZan obj:self info:@{@"commentid":self.commentModel.commentid,@"val":!self.zanBtn.selected?@"1":@"0",@"name":@"zan"}];
    else
        [CCNotice post:HYInfoCommentCellZan obj:self info:@{@"replyid":self.replyModel.replyid,@"val":!self.zanBtn.selected?@"1":@"0",@"name":@"zan"}];
    
}
//踩
- (void)actionForCai
{
    if ([[HYUserManager manager].user.account length] <= 1)
        return [[[self superViewController] navigationController] pushViewController:[NSClassFromString(@"HYLoginViewController") new] animated:YES];
    
    if (self.commentModel)
        [CCNotice post:HYInfoCommentCellCai obj:self info:@{@"commentid":self.commentModel.commentid,@"val":!self.caiBtn.selected?@"-1":@"0",@"name":@"cai"}];
    else
        [CCNotice post:HYInfoCommentCellCai obj:self info:@{@"replyid":self.replyModel.replyid,@"val":!self.caiBtn.selected?@"-1":@"0",@"name":@"cai"}];
}
//回复
- (void)actionForHuifu
{
    if ([[HYUserManager manager].user.account length] <= 1)
        return [[[self superViewController] navigationController] pushViewController:[NSClassFromString(@"HYLoginViewController") new] animated:YES];
    
    [CCNotice post:HYInfoCommentCellReply obj:nil info:@{@"obj":self.commentModel}];
}
- (void)actionForCallback:(NSNotification *)notify
{
    NSString *name = [notify.userInfo valueForKey:@"name"];
    if ([name hasPrefix:@"zan"])
    {
        int zan = [self.zanBtn.title intValue];
        int cai = [self.caiBtn.title intValue];
        
        self.zanBtn.selected = !self.zanBtn.selected;
        self.zanBtn.title = [NSString stringWithFormat:@"%d",(self.zanBtn.selected)?zan+1:zan>0?zan-1:zan];
        
        self.caiBtn.title = [NSString stringWithFormat:@"%d",(cai>0 && self.caiBtn.selected)?(cai-1):cai];
        self.caiBtn.selected = NO;
    }
    else
    {
        int cai = [self.caiBtn.title intValue];
        int zan = [self.zanBtn.title intValue];
        
        self.caiBtn.selected = !self.caiBtn.selected;
        self.caiBtn.title = [NSString stringWithFormat:@"%d",(self.caiBtn.selected)?cai+1:(cai>0)?cai-1:cai];
        
        self.zanBtn.title = [NSString stringWithFormat:@"%d",(zan>0 && self.zanBtn.selected)?(zan-1):zan];
        self.zanBtn.selected = NO;
    }
    self.commentModel.likecount = self.zanBtn.title;
    self.commentModel.unlikecount = self.caiBtn.title;
    self.commentModel.mystate = (!self.zanBtn.selected && !self.caiBtn.selected)?@"0":self.zanBtn.selected?@"1":@"-1";
}
- (void)dealloc
{
    [CCNotice rmObs:self name:HYInfoCommentCellCallBack obj:self];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
